# 将有序数组转换为二叉搜索树
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def sortedArrayToBST(self, nums) -> TreeNode:
        if len(nums) == 0:
            return None
        root_index = len(nums) // 2
        root = TreeNode(nums[root_index])
        root.left = self.sortedArrayToBST(nums[:root_index])
        root.right = self.sortedArrayToBST(nums[root_index+1:])
        return root



if __name__ == '__main__':
    so = Solution()
    nums = [-10,-3,0,5,9]
    r = so.sortedArrayToBST(nums)
    print(r)
